package com.ninong.ker.admin.sevice.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import com.ninong.ker.admin.domain.FwGroup;
import com.ninong.ker.admin.mapper.FwFormConfigMapper;
import com.ninong.ker.admin.mapper.FwGroupMapper;
import com.ninong.ker.admin.sevice.FwGroupService;
import com.ninong.ker.common.auth.SecurityAdmin;
import com.ninong.ker.common.tools.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


/**
 * @author jmz
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class FwGroupServiceImpl extends ServiceImpl<FwGroupMapper, FwGroup> implements FwGroupService {

	@Autowired
	private SecurityAdmin securityAdmin;
	@Autowired
	private FwFormConfigMapper fwFormConfigMapper;

    @Override
	public PageInfo<JSONObject> listPage(JSONObject dto) {
		PageHelper.startPage(dto.getInteger("currentPage"), dto.getInteger("pageSize"));
		if(StringUtils.isEmpty(dto.getString("formId"))){
			LambdaQueryWrapper<FwGroup> query = new LambdaQueryWrapper<>();
			query.eq(FwGroup::getDeleted, Constants.DEFAULT_YES);
			List<FwGroup> groups = baseMapper.selectList(query);
			PageInfo<FwGroup> page = new PageInfo<>(groups);
			List<JSONObject> data = JSONArray.parseArray(JSONObject.toJSONString(groups, SerializerFeature.WriteDateUseDateFormat), JSONObject.class);
			PageInfo<JSONObject> jsons = new PageInfo<>(data);
			jsons.setTotal(page.getTotal());
			return jsons;
		}else{
			Map<String,Object> map = Maps.newHashMap();
			map.put("deleted",Constants.DEFAULT_YES);
			map.put("formId",dto.getString("formId"));
			dto.remove("formId");
			dto.remove("currentPage");
			dto.remove("pageSize");
			map.put("params",dto);
			List<FwGroup> groups = baseMapper.selectGroupDataPage(map);
			PageInfo<FwGroup> page = new PageInfo<>(groups);
			List<JSONObject> data = new ArrayList<>();
			groups.forEach(value -> {
				JSONObject json =value.getValue();
				json.put("id", value.getId());
				json.put("createTime", value.getCreateTime());
				json.put("updateTime", value.getUpdateTime());
				json.put("name", value.getName());
				json.put("stock", value.getStock());
				data.add(json);
			});
			PageInfo<JSONObject> jsons = new PageInfo<>(data);
			jsons.setTotal(page.getTotal());
			return jsons;
		}
	}

	@Override
	public void delete(String[] ids) {
		List<String> idList = Arrays.asList(ids);
		idList.forEach(id -> {		
			FwGroup entity = new FwGroup();
			entity.setId(id);
			entity.setDeleted(false);
			baseMapper.updateById(entity);
		});
	}

	@Override
	public void create(JSONObject json) {
		FwGroup entity =new FwGroup();
		entity.setName(json.getString("name"));
		entity.setId(json.getString("id"));
		entity.setStock(json.getBigDecimal("stock"));
		entity.setFormId(json.getString("formId"));
		entity.setDeleted(Constants.DEFAULT_YES);
		json.remove("name");
		json.remove("stock");
		json.remove("id");
		json.remove("formId");
		entity.setValue(json);
		saveOrUpdate(entity);
	}

	@Override
	public JSONObject get(String id) {
		FwGroup entity = baseMapper.selectById(id);
		if (ObjectUtils.isEmpty(entity)) {
			entity = new FwGroup();
		}
		JSONObject json = entity.getValue();
		json.put("name",entity.getName());
		json.put("stock",entity.getStock());
		return json;
	}

	@Override
	public List<FwGroup> treeData() {
		LambdaQueryWrapper<FwGroup> query = new LambdaQueryWrapper<>();
		query.select(FwGroup::getId,FwGroup::getName);
		return baseMapper.selectList(query);
	}

}